package com.busi.ecCommplete;

import java.io.File;
import java.util.List;

import com.busi.utils.DictUtil;
import com.jfinal.aop.Duang;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;
import com.zcurd.common.DBTool;
import com.zcurd.common.ZcurdTool;
import com.zcurd.common.base.BaseController;
import com.zcurd.common.util.StringUtil;

/**
 * 合同管理
 */
public class EcCommpleteController extends BaseController {

	public void listPage() {
		setAttr("classify", DictUtil.dict.getDictList("ec_classify"));
		render("list.html");
	}

	public void listData() {
		Object[] queryParams = getQueryParams();
		String[] properties = (String[]) queryParams[0];
		String[] symbols = (String[]) queryParams[1];
		Object[] values = (Object[]) queryParams[2];
		String orderBy = getOrderBy();
		if (StringUtil.isEmpty(orderBy)) {
			orderBy = "id desc";
		}
		List<Record> list = DBTool.findByMultPropertiesDbSource("zcurd_busi", "t_b_ec_commplete", properties, symbols,
				values, orderBy, getPager());
		ZcurdTool.replaceDict(DictUtil.dict.getDictList("ec_classify"), list, "ec_type");
		renderDatagrid(list,
				DBTool.countByMultPropertiesDbSource("zcurd_busi", "t_b_ec_commplete", properties, symbols, values));
	}

	// 上传界面
	public void addPage() {
		setAttr("classify", DictUtil.dict.getDictList("ec_classify"));
		render("add.html");
	}

	// 上传
	public void add() {
		UploadFile file = getFile();
		int ecType = getParaToInt("ecType");
		EcCommplete model = getModel(EcCommplete.class, "model");
		EcCommpleteService ecCommpleteService = Duang.duang(EcCommpleteService.class);
		ecCommpleteService.uploadFile(file, model, ecType);
		renderSuccess();
	}

	// 删除
	public void delete() {
		Integer[] ids = getParaValuesToInt("id[]");
		for (Integer id : ids) {
			new EcCommplete().set("id", id).delete();
		}
		addOpLog("[合同管理] 删除");
		renderSuccess();
	}

	// 下载签订后的合同
	public void downloadFile() {
		long id = getParaToLong("id");
		EcCommplete ec = EcCommplete.me.findById(id);
		EcCommpleteService ecCommpleteService = Duang.duang(EcCommpleteService.class);
		File file = ecCommpleteService.downloadFile(ec);
		renderFile(file, ec.getStr("name"));
	}
}
